<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>TodoList</title>
  <script src="../asset/js/vue.js"></script>
</head>

<body>
  <div id="app">
    <input type="text" v-model="inputValue">
    <button v-on:click='handleBtnClick'>提交</button>
    <ul>
      <!-- <li v-for='item of list'>{{item}}</li> -->
      <todo-item v-for='(item,index) of list' v-bind:content='item' :index='index' @delete='handleItemDelete' :key='index'></todo-item>
    </ul>
  </div>
  <script>
    /* 全局组件 */
    /* Vue.component('TodoItem', {
      props: ['content'],
      template: `<li>{{content}}</li>`
    }) */
    /* 局部组件 */
    let TodoItem = {
      props: ['content', 'index'],
      template: `<li v-on:click='handleItemClick'>{{content}}</li>`,
      methods: {
        handleItemClick: function () {
          // console.log('index:', this.index)
          this.$emit('delete', this.index);
        }
      }
    }
    let app = new Vue({
      el: '#app',
      data: {
        list: [],
        inputValue: ''
      },
      /* 注册局部组件 */
      components: {
        'todo-item': TodoItem
      },
      methods: {
        handleBtnClick() {
          let newList = this.inputValue;
          this.list.push(newList)
          this.inputValue = ''
        },
        handleItemDelete(index) {
          // console.log(this.list[index])
          this.list.splice(index, 1);
        }
      }
    })
  </script>
</body>

</html>